package addressbook

import (
	"public_security/controllers"
	"public_security/models"
	"public_security/models/addressbook"
)

// 本所通讯 API
type AddressBookController struct {
	controllers.BaseController
}

func (i *AddressBookController) Prepare() {
	i.BasePrepare()
}

// @Title create
// @Description 创建通讯录
// @Param	body		body 	models.AddressBook	 true		"请求参数"
// @Success 200 success
// @router /create [post]
func (i *AddressBookController) Create() interface{} {
	art := new(models.AddressBook)
	err := i.RequestJsonScan(&art)
	if err != nil {
		return i.ToMessageFailed(nil, "参数错误")
	}
	err = addressbook.Create(art)
	if err != nil {
		return i.ToMessageFailed(nil, err.Error())
	}
	return i.ToMessageSuccess(nil, "创建成功")
}

// @Title update
// @Description 修改通讯录
// @Param	body		body 	models.AddressBook	 true		"请求参数"
// @Success 200 success
// @router /update [post]
func (i *AddressBookController) Update() interface{} {
	art := new(models.AddressBook)
	err := i.RequestJsonScan(&art)
	if err != nil {
		return i.ToMessageFailed(nil, "参数错误")
	}
	err = addressbook.Update(art)
	if err != nil {
		return i.ToMessageFailed(nil, err.Error())
	}
	return i.ToMessageSuccess(nil, "修改成功")
}

// @Title remove
// @Description 删除通讯录
// @Param	body		body 	models.AddressBook	 true		"请求参数"
// @Success 200 success
// @router /remove [post]
func (i *AddressBookController) Remove() interface{} {
	art := new(models.AddressBook)
	err := i.RequestJsonScan(&art)
	if err != nil {
		return i.ToMessageFailed(nil, "参数错误")
	}
	err = addressbook.Remove(art)
	if err != nil {
		return i.ToMessageFailed(nil, err.Error())
	}
	return i.ToMessageSuccess(nil, "删除成功")
}

// @Title query
// @Description 查询通讯录
// @Param	body		body 	models.AddressBook	 true		"请求参数"
// @Success 200 {object} models.AddressBook
// @router /query [post]
func (i *AddressBookController) Query() interface{} {
	art := new(models.AddressBook)
	err := i.RequestJsonScan(&art)
	if err != nil {
		return i.ToMessageFailed(nil, "参数错误")
	}
	results := addressbook.Query(art)
	return i.ToMessageSuccess(results, "查询成功")
}

// @Title createType
// @Description 创建分类
// @Param	body		body 	models.AddressBookType	 true		"请求参数"
// @Success 200 success
// @router /createType [post]
func (i *AddressBookController) CreateType() interface{} {
	art := new(models.AddressBookType)
	err := i.RequestJsonScan(&art)
	if err != nil {
		return i.ToMessageFailed(nil, "参数错误")
	}
	err = addressbook.CreateType(art)
	if err != nil {
		return i.ToMessageFailed(nil, err.Error())
	}
	return i.ToMessageSuccess(nil, "创建成功")
}

// @Title updateType
// @Description 修改分类
// @Param	body		body 	models.AddressBookType	 true		"请求参数"
// @Success 200 success
// @router /updateType [post]
func (i *AddressBookController) UpdateType() interface{} {
	art := new(models.AddressBookType)
	err := i.RequestJsonScan(&art)
	if err != nil {
		return i.ToMessageFailed(nil, "参数错误")
	}
	err = addressbook.UpdateType(art)
	if err != nil {
		return i.ToMessageFailed(nil, err.Error())
	}
	return i.ToMessageSuccess(nil, "修改成功")
}

// @Title removeType
// @Description 删除分类
// @Param	body		body 	models.AddressBookType	 true		"请求参数"
// @Success 200 success
// @router /removeType [post]
func (i *AddressBookController) RemoveType() interface{} {
	art := new(models.AddressBookType)
	err := i.RequestJsonScan(&art)
	if err != nil {
		return i.ToMessageFailed(nil, "参数错误")
	}
	err = addressbook.RemoveType(art)
	if err != nil {
		return i.ToMessageFailed(nil, err.Error())
	}
	return i.ToMessageSuccess(nil, "删除成功")
}

// @Title queryType
// @Description 查询分类
// @Param	body		body 	models.AddressBookType	 true		"请求参数"
// @Success 200 {object} models.AddressBookType
// @router /queryType [post]
func (i *AddressBookController) QueryType() interface{} {
	art := new(models.AddressBookType)
	err := i.RequestJsonScan(&art)
	if err != nil {
		return i.ToMessageFailed(nil, "参数错误")
	}
	results := addressbook.QueryType(art)
	return i.ToMessageSuccess(results, "查询成功")
}
